package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constrant.RedisConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckGroupService;
import com.itheima.service.SetmealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import redis.clients.jedis.JedisPool;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.*;

/**
 * @author FCJ
 * @date 2020-01-03 20:50
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Value("${out_put_path}")
    private String outPutPath;

    @Override
    public void addrelation(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.addSetmeal(setmeal);
        Integer setmealId = setmeal.getId();
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            for (Integer checkgroupId : checkgroupIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("setmealId", setmealId);
                map.put("checkgroupId", checkgroupId);
                setmealDao.addRelation(map);
                String fileName = setmeal.getImg();
                jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, fileName);

            }
        }
        generateMobileStaticHtml();
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Setmeal> page = setmealDao.findByCondition(queryString);
        List<Setmeal> setmeals = page.getResult();
        Long total = page.getTotal();
        PageResult pageResult = new PageResult(total, setmeals);
        return pageResult;
    }

    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    @Override
    public Setmeal finDetaldById(Integer id) {
        return setmealDao.finDetaldById(id);
    }

    @Override
    public Setmeal findSetmealById(Integer id) {
        return setmealDao.findSetmealById(id);
    }

    @Override
    public Map<String, Object> findSetmealCount() {
//        List<String> setmealNames=setmealDao.findAllName();
        List<Map<String, Object>> setmealCount = setmealDao.findSetmealCount();
        Map<String, Object> map = new HashMap<>();
        List<String> setmealNames = new ArrayList<>();
        for (Map map1 : setmealCount) {
            String name = (String) map1.get("name");
            setmealNames.add(name);
        }
        map.put("setmealNames", setmealNames);
        map.put("setmealCount", setmealCount);
        return map;
    }

    @Override
    public Setmeal findById(Integer id) {
        return null;
    }

    @Override
    public List<Integer> findGroupIdBySetmealId(Integer id) {

        return setmealDao.findGroupIdBySetmealId(id);
    }

    @Override
    public void update(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.updateSetmeal(setmeal);
        Integer id = setmeal.getId();
        setmealDao.deleteRelation(id);
        for (Integer checkGroupId:checkgroupIds){
            Map map=new HashMap<String,Object>();
            map.put("setmealId",id);
            map.put("checkgroupId",checkGroupId);
            setmealDao.addRelation(map);
        }
    }

    public void generateHtml(String teplateName, String htmlPageName, Map map) {
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        try {
            Template template = configuration.getTemplate(teplateName);
            Writer out = new FileWriter(new File(outPutPath + "/" + htmlPageName));
            template.process(map, out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //生成当前方法所需的静态页面
    public void generateMobileStaticHtml() {
        //生成也面前需要查询数据
        List<Setmeal> list = setmealDao.findAll();

        //需要生成套餐列表页面
        generaMobileSetmealListHtml(list);
        //需要生成套餐详情页面
        generateMobileSetmealDetailHtml(list);
    }

    //生成套餐列表静态页面
    public void generaMobileSetmealListHtml(List<Setmeal> list) {
        Map map = new HashMap();
        map.put("setmealList", list);
        generateHtml("mobile_setmeal.ftl", "m_setmeal.html", map);
    }

    //生成套餐详情静态页面（可能有多个）
    public void generateMobileSetmealDetailHtml(List<Setmeal> list) {
        for (Setmeal setmeal : list) {
            Map map = new HashMap();
            map.put("setmeal", setmealDao.finDetaldById(setmeal.getId()));
            generateHtml("mobile_setmeal_detail.ftl", "setmeal_detail_" + setmeal.getId() + ".html", map);
        }

    }

}
